package com.smart.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.smart.common.dao.AbstractDao;
import com.smart.domain.UserExtend;
import com.smart.dto.UserDto;
import com.smart.tools.ObjectHelper;

@Repository
public class UserExtendDao  extends AbstractDao<UserExtend>{

	/**
	 * 
	 * @Title: queryUserExtend 
	 * @Description: 查询用户扩展信息
	 * @author tangyan 
	 * @param conditions
	 * @return
	 */
	public List<UserExtend> queryUserExtend(Map<String, Object> conditions){
		StringBuffer hql = new 	StringBuffer(" from User u  where u.isDeleted = 0 ");
		if(ObjectHelper.isNotEmpty(conditions.get("userId"))){
			hql.append(" and u.userId = :userId ");
		}
		
		return this.findByHql(hql.toString(), conditions);
	}

	
	/**
	 * 
	 * @Title: queryUser 
	 * @Description: 查询用户
	 * @author tangyan 
	 * @param conditions
	 * @return
	 */
	public List<UserDto> queryUser(Map<String, Object> conditions){
		StringBuffer hql = new 	StringBuffer(" from User u , UserExtend ue where  u.id = ue.userId and ue.isDeleted = 0 and  u.isDeleted = 0 ");
		if(ObjectHelper.isNotEmpty(conditions.get("userName"))){
			hql.append(" and u.userName = :userName ");
		}
		
		if(ObjectHelper.isNotEmpty(conditions.get("identityCard"))){
			hql.append(" and u.identityCard = :identityCard ");
		}
		
		if(ObjectHelper.isNotEmpty(conditions.get("phoneNumber"))){
			hql.append(" and u.phoneNumber = :phoneNumber ");
		}
		
		Query query =	getSession().createQuery(hql.toString());
		query.setProperties(conditions);
		query.setResultTransformer(Transformers.aliasToBean(UserDto.class));
		return query.list();
		
	}

}
